package darknet.application.chat.impl.net.message;

import darknet.core.impl.net.AbstractMessage;
import darknet.core.spec.net.MessageHeader;
import darknet.core.spec.net.Node;

/**
 * A channel message broadcasted to all active nodes.
 *
 * This message is sent by the channel owner to all active nodes when the
 * owner receive a channel message.
 *
 * A node sent a message to the channel, and the channel broadcast the
 * message to all active nodes.
 * 
 * @author Yoyo
 */
public class BroadcastChannelMessage extends AbstractMessage {

    // TODO use a decorator for broadcasted message.

    /** The text message */
    private String text;
    
    /** The channel name */
    private String channelName;

    /** Original Poster */
    private Node originalPoster;
    
    /**
     * Initialise a new Broadcast channel message.
     * 
     * @param text	     The text content.
     * @param from	     The node hosting the channel.
     * @param to	     The destination node.
     * @param originalPoster The original poster
     */
    public BroadcastChannelMessage(MessageHeader header, Node from, Node to,
             String text, String channelName, Node originalPoster) {
	super(header, from, to);
	
	if(text == null || text.length() == 0) {
	    throw new IllegalArgumentException("message is invalid");
	}
	this.text = text;
	
	if (channelName == null || channelName.length() == 0) {
	    throw new IllegalArgumentException("channelName is invalid");
	}
	this.channelName = channelName;
	
	if (originalPoster == null) {
	    throw new IllegalArgumentException("originalPoster is null");
	}
	this.originalPoster = originalPoster;

        if (header == null) {
            throw new IllegalArgumentException("header is null");
        }
    }
    
    /**
     * Get the text message.
     * @return
     */
    public String getText() {
	return text;
    }
    
    public String getChannelName() {
	return channelName;
    }

    /**
     * 
     * @return
     */
    public Node getOriginalPoster() {
	return originalPoster;
    }

    @Override
    public String toString() {

        StringBuilder builder = new StringBuilder("BroadcastChannelMessage[");
        builder.append("super=");
        builder.append(super.toString());
        builder.append(", ");
        builder.append("text=");
        builder.append(text);
        builder.append(", ");
        builder.append("channelName=");
        builder.append(channelName);
        builder.append(", ");
        builder.append("originalPoster=");
        builder.append(originalPoster.toString());
        builder.append("]");

        return builder.toString();
    }
}
